package com.yogee.topseller.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.model.bean.CheckBean;
import com.yogee.topseller.adapter.DingyueAdapter;
import com.yogee.topseller.bean.DingyueBean;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.bean.ResultBean1;
import com.yogee.topseller.fragment.product.model.bean.ProductBean;
import com.yogee.topseller.fragment.product.model.bean.ProductListBean;
import com.yogee.topseller.fragment.zhoubian.DeleteDyFragment;
import com.yogee.topseller.fragment.zhoubian.DingyueFragment;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.EmptyRecyclerView;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;

public class DingyueListActivity extends HttpActivity implements RefreshUtil.OnRefreshListener {

    Unbinder unbinder;
    @BindView(R.id.title_white_left_back)
    LinearLayout titleWhiteLeftBack;
    @BindView(R.id.title_white_left_txt)
    TextView titleWhiteLeftTxt;
    @BindView(R.id.title_white_title)
    TextView titleWhiteTitle;
    @BindView(R.id.title_white_right_txt)
    TextView titleWhiteRightTxt;
    @BindView(R.id.tv_compare_btn)
    TextView tvCompareBtn;
    @BindView(R.id.title_white_right_add_img)
    ImageView titleWhiteRightAddImg;
    @BindView(R.id.activity_product_compare)
    LinearLayout activityProductCompare;
    @BindView(R.id.ll_confrim_layout)
    RelativeLayout ll_confrim_layout;
//    @BindView(R.id.body_refresh)
//    TwinklingRefreshLayout bodyRefresh;
    @BindView(R.id.body_recycler)
    EmptyRecyclerView bodyRecycler;
    @BindView(R.id.id_empty_view)
    View id_empty_view;
    @BindView(R.id.iv_empty)
    ImageView iv_empty;

    DingyueFragment dingyueFragment = new DingyueFragment();
    DeleteDyFragment deleteDyFragment = new DeleteDyFragment();
    private String selectProductId;
    private List<DingyueBean.DingyueInfo> productBeanList;
    DingyueAdapter adapter;
    private int selectNum = 0;//已选产品数量
    private boolean isRefresh = false;
    @Override
    public void onRefresh() {
//        isRefresh = true;
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                bodyRefresh.finishRefreshing();
//            }
//        }, 1500);
    }

    @Override
    public void onLoad() {
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                bodyRefresh.finishLoadmore();
//            }
//        }, 1500);
//        isRefresh = false;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_product_dingyue;
    }

    @Override
    protected void initView() {
        productBeanList = new ArrayList<>();
        titleWhiteLeftBack.setVisibility(View.VISIBLE);
        titleWhiteTitle.setText("我的订阅");
        titleWhiteRightTxt.setText("编辑");
        titleWhiteRightTxt.setVisibility(View.VISIBLE);
        titleWhiteRightAddImg.setVisibility(View.GONE);
        iv_empty.setImageResource(R.drawable.nodata6);
//        bodyRefresh.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
//        bodyRefresh.setHeaderView(new RefreshView(DingyueListActivity.this));
//        bodyRefresh.setBottomView(new RefreshView(DingyueListActivity.this));
//        bodyRefresh.setBottomHeight(50);

        bodyRecycler.setLayoutManager(new LinearLayoutManager(DingyueListActivity.this));
        bodyRecycler.setHasFixedSize(true);
        adapter = new DingyueAdapter(this);
        bodyRecycler.setAdapter(adapter);
        bodyRecycler.setEmptyView(id_empty_view);
        isRefresh = true;

//        adapter.addData();
    }

    private void initData(){

        HttpManager.getInstance().usersubscribeList(AppUtil.getUserId(DingyueListActivity.this)).compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<DingyueBean>() {
            @Override
            public void onNext(DingyueBean o) {
                if(o.getResult().equals("1")){
                    adapter.addData(o.getScribeList());
                }
            }
        }, this));

    }


    public void removeItem(String ids,final int position){
        if(pd!=null)
            pd.show();
        HttpManager.getInstance().userSubscribeSubscribe(ids).compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean1>() {
            @Override
            public void onNext(ResultBean1 o) {
                // 删除成功
                if(o.getResult().equals("1")){
                    adapter.romoveSelect(position);
                    ToastUtils.showToast(DingyueListActivity.this,"取消订阅成功");
                }
            }
        }, this));
    }

    public void shuaxin(String ids){
        if(pd!=null)
            pd.show();
        HttpManager.getInstance().userSubscribeDelete(ids).compose(bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean1>() {
            @Override
            public void onNext(ResultBean1 o) {
                // 删除成功
                if(o.getResult().equals("1")){
                    adapter.shuaxin();
                    ToastUtils.showToast(DingyueListActivity.this,"删除成功");
                }
            }
        }, this));
    }
    /**
     * 删除
     * @param ids
     * @param position
     */
    public void dingyueDelete(String ids,final int position){
        Bundle args = new Bundle();
        args.putString("ids",ids);
        args.putInt("position", position);
        dingyueFragment.setArguments(args);
        dingyueFragment.setCancelable(false);
        dingyueFragment.show(getFragmentManager(),"");
    }

    public void dingyueDelete1(String ids,final int count){
        Bundle args = new Bundle();
        args.putString("ids",ids);
        args.putInt("count", count);
        deleteDyFragment.setArguments(args);
        deleteDyFragment.setCancelable(false);
        deleteDyFragment.show(getFragmentManager(),"");
    }


    @Override
    protected void onResume() {
        super.onResume();
//        loadCompareData();
        initData();
    }

    /**
     * 获取对比产品列表
     */
    private void loadCompareData() {
        HttpManager.getInstance().loadCompareProducts(AppUtil.getUserId(this)).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ProductListBean>() {
            @Override
            public void onNext(ProductListBean o) {
                if ("1".equals(o.getResult())) {
                    if (null != o.getProductList() && o.getProductList().size() > 0) {
//                        addCompareList(o.getProductList());
                    }
                }
            }
        }, this));
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @OnClick({R.id.title_white_left_back,R.id.title_white_left_txt,R.id.title_white_right_txt,R.id.ll_confrim_layout})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.img_product_compare:
//                ToastUtils.showToast(ProductCompareActivity.this,"搜索");
//                Intent intent = new Intent();
//                intent.setClass(ProductCompareActivity.this,SearchAndSelectActivity.class);
//                startActivity(intent);
                startActivity(new Intent(DingyueListActivity.this, CompareProductActivity.class).putExtra("type", "0"));
                break;
            case R.id.title_white_left_back:
                if (AppUtil.isFastDoubleClick(500)) {
                    return;
                }
                finish();
                break;
            case R.id.title_white_left_txt:
                //编辑状态取消按钮事件
                titleWhiteLeftBack.setVisibility(View.VISIBLE);
//                titleWhiteRightAddImg.setVisibility(View.VISIBLE);
                titleWhiteLeftTxt.setText("");
                titleWhiteLeftTxt.setVisibility(View.GONE);
                titleWhiteRightTxt.setText("编辑");
                ll_confrim_layout.setVisibility(View.GONE);
                adapter.setEdit(false);
//                tvCompareBtn.setText("开始对比");
//                ivCompareDelete.setVisibility(View.GONE);
                break;
            case R.id.title_white_right_txt:
                if ("编辑".equals(titleWhiteRightTxt.getText().toString())) {
                    //打开编辑状态
//                    titleWhiteRightAddImg.setVisibility(View.GONE);
                    titleWhiteLeftBack.setVisibility(View.GONE);
                    titleWhiteLeftTxt.setText("取消");
                    titleWhiteLeftTxt.setVisibility(View.VISIBLE);
                    titleWhiteRightTxt.setText("全选");
                    tvCompareBtn.setText("删除");
                    ll_confrim_layout.setVisibility(View.VISIBLE);
                    adapter.setEdit(true);
//                    ivCompareDelete.setVisibility(View.VISIBLE);
//                    noSelect();
                } else if ("全选".equals(titleWhiteRightTxt.getText().toString())) {
                    adapter.setSelectAll(true);
                    titleWhiteRightTxt.setText("取消全选");
                } else if ("取消全选".equals(titleWhiteRightTxt.getText().toString())) {
                    adapter.setSelectAll(false);
                    titleWhiteRightTxt.setText("全选");
//                    ll_confrim_layout.setVisibility(View.     GONE);
                }
                break;
            case R.id.title_white_right_add_img:
                //产品添加对比的 " + "
                startActivity(new Intent(this, CompareProductActivity.class).putExtra("type", "0").putExtra("id", AppUtil.getUserId(this)));
                break;
            case R.id.ll_confrim_layout:
                List<String> stringList = adapter.getSelect();
                String str = "";
                if(stringList!=null&&stringList.size()>0){
                    for(int i=0;i<stringList.size();i++){
                        if(i==0){
                            str+=stringList.get(i);
                        }else{
                            str+= ","+stringList.get(i);
                        }
                    }
                    dingyueDelete1(str,stringList.size());
                }else{
                    ToastUtils.showToast(DingyueListActivity.this,"至少选择一款产品");
                }
                break;
            case R.id.tv_compare_btn:
                if (tvCompareBtn.getText().toString().equals("开始对比")) {
//                    if (TextUtils.isEmpty(selectProductIdM())) {
//                        ToastUtils.showToast(DingyueListActivity.this, "请选择产品");
//                        return;
//                    }
                    if (selectNum < 2){
                        selectNum = 0;
                        ToastUtils.showToast(DingyueListActivity.this, "请至少选择两款产品");
                        return;
                    }else if (selectNum >3){
                        selectNum = 0;
                        ToastUtils.showToast(DingyueListActivity.this, "最多能选择三款产品");
                        return;
                    }else{
//                        startCompareProduct();
                    }
                } else if (tvCompareBtn.getText().toString().equals("删除")) {
//                    deleteCompareProduct();
                }
                break;
        }
    }




}
